<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace Rtgm\sm;

class RtSm4
{
    protected $sm4;
    protected $keyLen = 16;
    protected $ivLen = 16;
    public function __construct($key)
    {
        $this->sm4 = new \Rtgm\smecc\SM4\Sm4($key);
    }
    public function encrypt($data, $type = "sm4", $iv = "", $formatOut = "hex")
    {
        if($type != "sm4-ecb") {
            $this->check_iv($iv);
        }
        $ret = "";
        switch ($type) {
            case "sm4":
            case "sm4-cbc":
                $data = $this->mystr_pad($data, $this->keyLen);
                $ret = $this->sm4->enDataCbc($data, $iv);
                break;
            case "sm4-ecb":
                $data = $this->mystr_pad($data, $this->keyLen);
                $ret = $this->sm4->enDataEcb($data);
                break;
            case "sm4-ctr":
                $ret = $this->sm4->enDataCtr($data, $iv);
                break;
            case "sm4-ofb":
                $ret = $this->sm4->enDataOfb($data, $iv);
                break;
            case "sm4-cfb":
                $ret = $this->sm4->enDataCfb($data, $iv);
                if($formatOut == "hex") {
                    return bin2hex($ret);
                }
                if($formatOut == "base64") {
                    return base64_encode($ret);
                }
                return $ret;
                break;
            default:
                throw new \Exception("bad type");
        }
    }
    public function decrypt($data, $type = "sm4", $iv = "", $formatInput = "hex")
    {
        if($type != "sm4-ecb") {
            $this->check_iv($iv);
        }
        if($formatInput == "hex") {
            $data = hex2bin($data);
        } elseif($formatInput == "base64") {
            $data = base64_decode($data);
        }
        switch ($type) {
            case "sm4":
            case "sm4-cbc":
                $ret = $this->sm4->deDataCbc($data, $iv);
                $ret = $this->mystr_unpad($ret);
                break;
            case "sm4-ecb":
                $ret = $this->sm4->deDataEcb($data);
                $ret = $this->mystr_unpad($ret);
                break;
            case "sm4-ctr":
                $ret = $this->sm4->deDataCtr($data, $iv);
                break;
            case "sm4-ofb":
                $ret = $this->sm4->deDataOfb($data, $iv);
                break;
            case "sm4-cfb":
                $ret = $this->sm4->deDataCfb($data, $iv);
                return $ret;
                break;
            default:
                throw new \Exception("bad type");
        }
    }
    protected function mystr_pad($data, $len = 16)
    {
        $n = $len - strlen($data) % $len;
        return $data . str_repeat(chr($n), $n);
    }
    protected function mystr_unpad($data)
    {
        $n = ord(substr($data, -1));
        return substr($data, 0, -1 * $n);
    }
    protected function check_iv($iv)
    {
        if(strlen($iv) != $this->ivLen) {
            throw new \Exception("bad iv");
        }
    }
}

?>